home *** CD-ROM | disk | FTP | other *** search
/ Utilities Professional 1-1500 / Utilities Professional 1-1500 (1994)(WPD)[!].iso / 12511500 / var1461.dms / var1461.adf / IncludeFonts / PrintFont.c < prev    next >
C/C++ Source or Header  |  1992-05-07  |  20KB  |  588 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Graphics                Amiga C Club       */
  7. /* Chapter: Include Fonts               Tulevagen 22       */
  8. /* File:    PrintFont.c                 181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-05-01                                       */
  11. /* Version: 1.10                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20.  
  21.  
  22. /****************************************************************/
  23. /*                                                              */
  24. /* PrintFont loads a specified diskfont and prints the complete */
  25. /* definition of it in 'C'. This printout can be included and   */
  26. /* compiled with your own programs, and you will then be able   */
  27. /* to use the font without having to bother about loading it.   */
  28. /* Perfect for smaller utilities and games!                     */
  29. /*                                                              */
  30. /* If you wite a large program that is using a lot of different */
  31. /* fonts it is best to load these fonts as normal, and not      */
  32. /* include all of them with help of this utility. The advantage */
  33. /* with normal diskfonts is that several programs can use the   */
  34. /* same fonts, and thus a lot of memory is saved. If the font   */
  35. /* is included together with the program code, no other program */
  36. /* can use that font. However, for small utilities and games it */
  37. /* is better to include the font rather than messing around     */
  38. /* with external files that may not exist.                      */
  39. /*                                                              */
  40. /* Many thanks to Michael Loughman who told me about his        */
  41. /* excellent idea to make a program that converts fonts into    */
  42. /* normal 'C' code. (What should we include next?)              */
  43. /*                                                              */
  44. /*                                                              */
  45. /*                 I N S T R U C T I O N S                      */
  46. /*                 -----------------------                      */
  47. /*                                                              */
  48. /* PrintFont converts disk fonts to 'C' code. The font may be   */
  49. /* of any size, proportional or non-proportional, and in any    */
  50. /* style. PrintFont does not handle coloured fonts, although    */
  51. /* this will soon be changed. If the font is non-proportional   */
  52. /* (fixed size), PrintFont will write two arrays, one for the   */
  53. /* font data (graphics) and one which contains information      */
  54. /* about where each character is in the font data, and how wide */
  55. /* each bit definition of the character is. After these two     */
  56. /* arrays a complete pre-initialized TextFont structure is      */
  57. /* written.                                                     */
  58. /*                                                              */
  59. /* If the font is proportional (variable size), PrintFont will  */
  60. /* add two more arrays. The first array contains the width of   */
  61. /* the box in which each character is printed, while the second */
  62. /* array contains the kerning data for each character. (The     */
  63. /* kerning value is the number of pixels each character is      */
  64. /* moved from the left left side of the box.)                   */
  65. /*                                                              */
  66. /* You need to tell PrintFont three things. First you must of   */
  67. /* course tell it which font yu want to convert. Secondly which */
  68. /* size of the font you want. (PrintFont will try to load the   */
  69. /* font which best matches your requirements.) Finally you need */
  70. /* to tell it what name the arrays and structure should have.   */
  71. /* (The name will be added to all arrays and the structure. For */
  72. /* example, if you set the name to "Nice", the font data array  */
  73. /* will then be called "NiceData" and the structure "NiceFont"  */
  74. /* and so on...)                                                */
  75. /*                                                              */
  76. /* This is how you call PrintFont: (without quotations)         */
  77. /*                                                              */
  78. /*   PrintFont > "file.c" "font" "size" "name"                  */
  79. /*                                                              */
  80. /* "file.c": The name of the file where all 'C' code will be    */
  81. /*           stored. Note the "arrow" in front of the name, it  */
  82. /*           tells DOS that all output should be stored in the  */
  83. /*           file "file.c".                                     */
  84. /*                                                              */
  85. /* "font":   The name of the font you want to convert.          */
  86. /*                                                              */
  87. /* "size":   The size (number of points) of the font you want   */
  88. /*           to convert.                                        */
  89. /*                                                              */
  90. /* "name":   The name of the structure and arrays.              */
  91. /*                                                              */
  92. /* For example. To convert the font "Times" of the size 18      */
  93. /* points to 'C' code, do like this: (The 'C' code will be      */
  94. /* stored in file "FontFile.c", and the TextFont structure      */
  95. /* will be called "NewsletterFont".)                            */
  96. /*                                                              */
  97. /*   PrintFont > FontFile.c Times 18 Newsletter                 */
  98. /*                                                              */
  99. /*                                                              */
  100. /* Once the font has been converted you can immediately start   */
  101. /* to use it. To change the RastPort's font to your new         */
  102. /* "NewsletterFont", simply call the function SetFont().        */
  103. /*                                                              */
  104. /* Synopsis:  error = SetFont( rast_port, font );               */
  105. /*                                                              */
  106. /* error:     (long) If SetFond could not change the RastPort's */
  107. /*            font it returns a non zero value. If it could     */
  108. /*            change font successfully it returns 0.            */
  109. /*                                                              */
  110. /* rast_port: (struct RastPort *) Pointer to the RastPort which */
  111. /*            should use the new font.                          */
  112. /*                                                              */
  113. /* font:      (struct TextFont *) Pointer to the font you have  */
  114. /*            converted (or opened as usual).                   */
  115. /*                                                              */
  116. /* error = SetFont( &my_rast_port, &NewsletterFont )            */
  117. /* if( error )                                                  */
  118. /*   printf( "Could not change font!\n" );                      */
  119. /*                                                              */
  120. /****************************************************************/
  121.  
  122.  
  123. #include <intuition/intuition.h>
  124.  
  125.  
  126. struct Intuition *IntuitionBase = NULL; /* Running under Intuition. */
  127. struct GfxBase *GfxBase = NULL;         /* Move() and Text().       */
  128. struct Library *DiskfontBase = NULL;    /* OpenDiskFont() etc.      */
  129.  
  130.  
  131.  
  132. /* The new font's attributes: */
  133. struct TextAttr font_attr;
  134.  
  135. /* Pointer to our new font: */
  136. struct TextFont *font = NULL;
  137.  
  138. /* Declare a pointer to a Window structure: */ 
  139. struct Window *my_window = NULL;
  140.  
  141. /* Declare and initialize your NewWindow structure: */
  142. struct NewWindow my_new_window=
  143. {
  144.   0,             /* LeftEdge    x position of the window. */
  145.   12,            /* TopEdge     y positio of the window. */
  146.   500,           /* Width       500 pixels wide. */
  147.   0,             /* Height      Will be changed. */
  148.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  149.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  150.   NULL,          /* IDCMPFlags  No IDCMP flags. */
  151.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  152.   WINDOWDRAG|    /*             Drag gadget. */
  153.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  154.   ACTIVATE,      /*             The window should be Active when opened. */
  155.   NULL,          /* FirstGadget No Custom gadgets. */
  156.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  157.   "PrintFont V1.0  Anders Bjerin  Amiga C Club", /* Title */
  158.   NULL,          /* Screen      Connected to the Workbench Screen. */
  159.   NULL,          /* BitMap      No Custom BitMap. */
  160.   0,             /* MinWidth    No sizing gadget. */
  161.   0,             /* MinHeight          -"-        */
  162.   0,             /* MaxWidth           -"-        */
  163.   0,             /* MaxHeight          -"-        */
  164.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  165. };
  166.  
  167.  
  168.  
  169. /* Declare our functions: */
  170. void main( int argc, char *argv[] );
  171. void clean_up( STRPTR );
  172. void PrintFont( STRPTR name );
  173. void PrintFontData( STRPTR name );
  174. void PrintFontLoc( STRPTR name );
  175. void PrintFontSpace( STRPTR name );
  176. void PrintFontKern( STRPTR name );
  177. void PrintFontStructure( STRPTR name );
  178.  
  179.  
  180.  
  181. void main(
  182.   int argc,
  183.   char *argv[]
  184. )
  185. {
  186.   /* Store the name of the font here: (25 char + NULL. */
  187.   /* Font name plus extension ".font")                 */
  188.   char font_name[ 26 ];
  189.  
  190.  
  191.  
  192.   /* Open the necessary libraries: */
  193.  
  194.   /* Open the Intuition Library: */
  195.   IntuitionBase = (struct IntuitionBase *)
  196.     OpenLibrary( "intuition.library", 0 );
  197.   if( !IntuitionBase )
  198.     clean_up( "Could not open Intuition library!" );
  199.  
  200.   /* Open the Graphics library: */
  201.   GfxBase = (struct GfxBase *)
  202.     OpenLibrary( "graphics.library", 0 );
  203.   if( !GfxBase )
  204.     clean_up( "Could not open Graphics library!" );
  205.  
  206.   /* Open the DiskFont library: */
  207.   DiskfontBase = (struct DiskfontBase *)
  208.     OpenLibrary( "diskfont.library", 0 );
  209.   if( !DiskfontBase )
  210.     clean_up( "Could not open Diskfont library!" );
  211.  
  212.  
  213.  
  214.   /* Check that the names are not too long: */
  215.   if( strlen( argv[1] ) > 25 )
  216.     clean_up( "Font name may not be longer than 25 characters!" );
  217.   if( strlen( argv[3] ) > 25 )
  218.     clean_up( "Structure name may not be longer than 25 characters!" );
  219.  
  220.   /* Check if the program was started with three arguments: */
  221.   /* (Three arguments plus program name.)                   */
  222.   if( argc == 4 )
  223.   {
  224.     /* Correct number of arguments! */
  225.     
  226.     /* Add the extension ".font": */
  227.     strcpy( font_name, argv[ 1 ] );
  228.     strcat( font_name, ".font" );
  229.  
  230.  
  231.  
  232.     /* Set desired font attributes: */
  233.     font_attr.ta_Name = font_name;        /* Name of the font.  */
  234.     font_attr.ta_YSize = atoi( argv[2] ); /* Number of points.  */
  235.     font_attr.ta_Style = FS_NORMAL;       /* Try with normal.   */
  236.     font_attr.ta_Flags = FPF_DISKFONT;    /* It is a disk font. */
  237.  
  238.  
  239.  
  240.     /* Try to open a disk font: */
  241.     font = (struct TextFont *)
  242.       OpenDiskFont( &font_attr );
  243.  
  244.     /* Have we opened the font successfully? */
  245.     if( !font )
  246.       clean_up( "Could not open the font!" );
  247.  
  248.  
  249.  
  250.     /* Set the height of the window: */
  251.     my_new_window.Height = font->tf_YSize + 30;
  252.  
  253.     /* Open a window in which we will display the new font: */
  254.     my_window = (struct Window *) OpenWindow( &my_new_window );
  255.   
  256.     /* Have we opened the window succesfully? */
  257.     if(my_window == NULL)
  258.       clean_up( "Could not open the window!" );
  259.  
  260.  
  261.  
  262.     /* Set colour and draw mode: */
  263.     SetAPen( my_window->RPort, 1 );
  264.     SetDrMd( my_window->RPort, JAM1 );
  265.  
  266.     /* Change the window's default font: */
  267.     SetFont( my_window->RPort, font );
  268.  
  269.     /* Position the cursor, and print some characters: */
  270.     Move( my_window->RPort, 10, font->tf_Baseline + 20 );
  271.     Text( my_window->RPort, "1234567890 ABCDE abcde", 22 );
  272.  
  273.  
  274.  
  275.     /* Convert to 'C' code: */
  276.     PrintFont( argv[3] );
  277.   }
  278.   else
  279.   {
  280.     /* Since the program was not started with three arguments */
  281.     /* (program name + three arguments), give the user some   */
  282.     /* short instructions on how to use the program.          */
  283.  
  284.     /* Started from Workbench? */
  285.     if( !argc )
  286.       printf( "Sorry, can not be used from Worbench." );
  287.     else
  288.     {
  289.       /* Started from CLI: */
  290.       printf( "Incorrect number of arguments!\n" );
  291.       printf( "Usage: PrintFont > \"file.c\" \"font\" \"size\" \"name\"\n" );
  292.       printf( "Ex: PrintFont > FontFile.c Times 18 Newsletter\n" );
  293.     }
  294.   }
  295.  
  296.   /* The End: */
  297.   clean_up( "" );
  298. }
  299.  
  300.  
  301.  
  302. /* Clears and quits: */
  303. void clean_up( STRPTR message )
  304. {
  305.   /* Close the window: */
  306.   if( my_window )
  307.     CloseWindow( my_window );
  308.  
  309.   /* Close the font: */
  310.   if( font )
  311.     CloseFont( font );
  312.  
  313.   /* Close the Disk Font Library: */
  314.   if( DiskfontBase )
  315.     CloseLibrary( DiskfontBase );
  316.  
  317.   /* Close the Graphics Library: */
  318.   if( GfxBase )
  319.     CloseLibrary( GfxBase );
  320.  
  321.   /* Close the IntuitionLibrary: */
  322.   if( IntuitionBase )
  323.     CloseLibrary( IntuitionBase );
  324.   
  325.   /* Print any message: */
  326.   printf( "%s\n", message );
  327.  
  328.   /* Quit: */
  329.   exit();
  330. }
  331.  
  332.  
  333.  
  334. void PrintFont( STRPTR name )
  335. {
  336.   /* Start the code with some information about the font: */
  337.   printf( "/******************************************/\n" );
  338.   printf( "/*     FontData prepared by PrintFont     */\n" );
  339.   printf( "/*     ------------------------------     */\n" );
  340.   printf( "/* Anders Bjerin             Amiga C Club */\n" );
  341.   printf( "/*                                        */\n" );
  342.   printf( "/* Font name:   %25s */\n",
  343.     font->tf_Message.mn_Node.ln_Name );
  344.   printf( "/* Struct name: %25s */\n",
  345.     name );
  346.   printf( "/* Height:      %25d */\n",
  347.     font->tf_YSize );
  348.   printf( "/* Characters:  %19d - %3d */\n",
  349.     font->tf_LoChar, font->tf_HiChar );
  350.   printf( "/******************************************/\n\n" );
  351.  
  352.   printf( "#include <exec/types.h>\n" );
  353.   printf( "#include <graphics/text.h>\n\n" );
  354.  
  355.   /* Print the font data if it exist: */
  356.   if( font->tf_CharData )
  357.     PrintFontData( name );
  358.  
  359.   /* Print the font location and width if it exist: */
  360.   if( font->tf_CharLoc )
  361.     PrintFontLoc( name );
  362.  
  363.   /* Print the space information if it exist: */
  364.   if( font->tf_CharSpace )
  365.     PrintFontSpace( name );
  366.  
  367.   /* Print the kerning data if it exist: */
  368.   if( font->tf_CharKern )
  369.     PrintFontKern( name );
  370.  
  371.   /* Print the TextFont structure: */
  372.   PrintFontStructure( name );
  373.  
  374.   /* End the code: */
  375.   printf( "/******************************************/\n\n" );
  376. }
  377.  
  378.  
  379.  
  380. /* Print the font data (graphics): */ 
  381. void PrintFontData( STRPTR name )
  382. {
  383.   UWORD x, y;
  384.   UWORD row, col;
  385.   UWORD *ptr;
  386.  
  387.  
  388.   /* Calculate the number of columns of bytes: */
  389.   col = font->tf_Modulo / 2;
  390.  
  391.   /* The number of lines (rows): */
  392.   row = font->tf_YSize;
  393.  
  394.   /* Pointer to the font data: */
  395.   ptr = (UWORD *) font->tf_CharData;
  396.  
  397.  
  398.   /* Start to print: */
  399.   printf( "/* The font data: */\n" );
  400.   printf( "static UWORD %sData[%d]=\n{", name, col*row );
  401.   
  402.  
  403.   /* Row after row: */
  404.   for( y = 0; y < row; y++ )
  405.   {
  406.     /* New row: */
  407.     printf( "\n  /* Row %d: */", y );
  408.  
  409.     /* Column after column: */
  410.     for( x = 0; x < col; x++ )
  411.     {
  412.       /* Start on a new line after eight words: */
  413.       if( x % 8 == 0 )
  414.         printf( "\n  " );
  415.  
  416.       /* Print one word: */
  417.       printf( "0x%04X%s", *ptr, y == row-1 && x == col-1 ? "" : "," );
  418.  
  419.       /* Step one word foreward: */
  420.       ptr++;
  421.     }
  422.   }
  423.   printf( "\n};\n\n" );
  424. }
  425.  
  426.  
  427.  
  428. /* Print the location array: */
  429. void PrintFontLoc( STRPTR name )
  430. {
  431.   UWORD loop; 
  432.   UWORD nr;
  433.   ULONG *ptr;
  434.  
  435.   /* Number of characters: */
  436.   nr = font->tf_HiChar - font->tf_LoChar + 1;
  437.  
  438.   /* Pointer to the space array: */
  439.   ptr = (ULONG *) font->tf_CharLoc;
  440.  
  441.   /* Start to print: */
  442.   printf( "/* The location and width of each character: */\n" );
  443.   printf( "static ULONG %sLoc[%d]=\n{", name, nr );
  444.   
  445.   /* All characters: */
  446.   for( loop = 0; loop < nr; loop++ )
  447.   {
  448.     /* Start on a new line after fourth words: */
  449.     if( loop % 4 == 0 )
  450.       printf( "\n  " );
  451.  
  452.     /* Print two words, offset and width: (long) */
  453.     printf( "0x%08X%s", *ptr, loop == nr-1 ? "" : "," );
  454.  
  455.     /* Step one long (two words) foreward: */
  456.     ptr++;
  457.   }
  458.   printf( "\n};\n\n" );
  459. }
  460.  
  461.  
  462.  
  463. /* Print the space (box width) array: */
  464. void PrintFontSpace( STRPTR name )
  465. {
  466.   UWORD loop; 
  467.   UWORD nr;
  468.   UWORD *ptr;
  469.  
  470.   /* Number of characters: */
  471.   nr = font->tf_HiChar - font->tf_LoChar + 1;
  472.  
  473.   /* Pointer to the space array: */
  474.   ptr = (UWORD *) font->tf_CharSpace;
  475.  
  476.   /* Start to print: */
  477.   printf( "/* The width of each character's box: */\n" );
  478.   printf( "static UWORD %sSpace[%d]=\n{", name, nr );
  479.   
  480.   /* All characters: */
  481.   for( loop = 0; loop < nr; loop++ )
  482.   {
  483.     /* Start on a new line after eight words: */
  484.     if( loop % 8 == 0 )
  485.       printf( "\n  " );
  486.  
  487.     /* Print own word: */
  488.     printf( "0x%04X%s", *ptr, loop == nr-1 ? "" : "," );
  489.  
  490.     /* Step one word foreward: */
  491.     ptr++;
  492.   }
  493.   printf( "\n};\n\n" );
  494. }
  495.  
  496.  
  497.  
  498. /* Print the kerning array: */
  499. void PrintFontKern( STRPTR name )
  500. {
  501.   UWORD loop; 
  502.   UWORD nr;
  503.   UWORD *ptr;
  504.  
  505.   /* Number of characters: */
  506.   nr = font->tf_HiChar - font->tf_LoChar + 1;
  507.  
  508.   /* Pointer to the kern array: */
  509.   ptr = (UWORD *) font->tf_CharKern;
  510.  
  511.   /* Start to print: */
  512.   printf( "/* The width of each character's box: */\n" );
  513.   printf( "static UWORD %sKern[%d]=\n{", name, nr );
  514.   
  515.   /* All characters: */
  516.   for( loop = 0; loop < nr; loop++ )
  517.   {
  518.     /* Start on a new line after eight words: */
  519.     if( loop % 8 == 0 )
  520.       printf( "\n  " );
  521.  
  522.     /* Print own word: */
  523.     printf( "0x%04X%s", *ptr, loop == nr-1 ? "" : "," );
  524.  
  525.     /* Step one word foreward: */
  526.     ptr++;
  527.   }
  528.   printf( "\n};\n\n" );
  529. }
  530.  
  531.  
  532.  
  533. /* Print the TextFont structure: */
  534. void PrintFontStructure( STRPTR name )
  535. {
  536.   printf( "/* The text font structure: */\n" );
  537.   printf( "struct TextFont %sFont=\n", name );
  538.   printf( "{\n" );
  539.   printf( "  { /* Message */\n" );
  540.   printf( "    { /* Node */\n" );
  541.   printf( "      NULL,    /* ln_Succ */\n" );
  542.   printf( "      NULL,    /* ln_Pred */\n" );
  543.   printf( "      NT_FONT, /* ln_Type */\n" );
  544.   printf( "      0,       /* ln_Pri */\n" );
  545.   printf( "      \"%s\" /* ln_Name */\n", font->tf_Message.mn_Node.ln_Name );
  546.   printf( "    },\n" );
  547.   printf( "    NULL,      /* mn_ReplyPort */\n" );
  548.   printf( "    %5d      /* mn_Length */\n", font->tf_Message.mn_Length );
  549.   printf( "  },\n" );
  550.   printf( "  %5d, /* tf_YSize */\n", font->tf_YSize );
  551.   printf( "  %5d, /* tf_Style */\n", font->tf_Style );
  552.   printf( "  %5d, /* tf_Flags */\n", font->tf_Flags );
  553.   printf( "  %5d, /* tf_XSize */\n", font->tf_XSize );
  554.   printf( "  %5d, /* tf_Baseline */\n", font->tf_Baseline );
  555.   printf( "  %5d, /* tf_BoldSmear */\n", font->tf_BoldSmear );
  556.   printf( "      0, /* tf_Accessors */\n" );
  557.   printf( "  %5d, /* tf_LoChar */\n", font->tf_LoChar );
  558.   printf( "  %5d, /* tf_HiChar */\n", font->tf_HiChar );
  559.  
  560.   /* Pointer to the font data: */
  561.   if( font->tf_CharData )
  562.     printf( "  (APTR) &%sData,  /* tf_CharData */\n", name );
  563.   else
  564.     printf( "   NULL, /* tf_CharData */\n" );
  565.     
  566.   printf( "  %5d, /* tf_Modulo */\n", font->tf_Modulo );
  567.  
  568.   /* Pointer to the array of location and width of each character: */
  569.   if( font->tf_CharLoc )
  570.     printf( "  (APTR) &%sLoc,   /* tf_CharLoc */\n", name );
  571.   else
  572.     printf( "   NULL, /* tf_CharLoc */\n" );
  573.  
  574.   /* Pointer to the spcae array: */
  575.   if( font->tf_CharSpace )
  576.     printf( "  (APTR) &%sSpace, /* tf_CharSpace */\n", name );
  577.   else
  578.     printf( "   NULL, /* tf_CharSpace */\n" );
  579.  
  580.   /* Pointer to the kerning array: */
  581.   if( font->tf_CharKern )
  582.     printf( "  (APTR) &%sKern,  /* tf_CharKern */\n", name );
  583.   else
  584.     printf( "   NULL, /* tf_CharKern */\n" );
  585.  
  586.   printf( "};\n\n" );
  587. }
  588.